package com.dindang.propertyfee.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dindang.propertyfee.R;
import com.dindang.propertyfee.mvp.ui.adapter.pager.PropertyFeeFragmentAdapter;
import com.dindang.propertyfee.mvp.ui.fragment.PropertyFeeFragment;
import com.dindang.propertyfee.mvp.ui.fragment.PropertyFeeRecordFragment;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;

/**
 * Created by cxw on & 2018/11/21
 * 物业收费
 */
@Route(path = "/propertyfee/activity/PropertyFeeActivity")
public class PropertyFeeActivity extends BaseActivity {

    private String[] mTitles = {"物业收费", "收款记录"};

    ImageView mIvBack;
    SegmentTabLayout mTablayout;
    ImageView mIvAdvanceSearch;
    ViewPager mViewpager;

    private AppComponent mAppComponent;

    private PropertyFeeFragmentAdapter mAdapter;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        this.mAppComponent = appComponent;
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.propertyfee_activity_property_fee;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        initView();
        mHandler.sendEmptyMessage(0);
        initListener();
    }

    private void initView() {
        mIvBack = findViewById(R.id.propertyfee_iv_back);
        mTablayout = findViewById(R.id.propertyfee_tablayout);
        mIvAdvanceSearch = findViewById(R.id.propertyfee_iv_advance_search);
        mViewpager = findViewById(R.id.propertyfee_viewpager);
    }

    private void initListener() {
        mIvBack.setOnClickListener(v -> finish());

        mTablayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewpager.setCurrentItem(position, false);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float v, int i1) {
                switch (position) {
                    case 0:
                        PropertyFeeFragment fragment0 = (PropertyFeeFragment) mAdapter.getItem(0);
                        fragment0.setData("init");
                        break;
                    case 1:
                        PropertyFeeRecordFragment fragment1 = (PropertyFeeRecordFragment) mAdapter.getItem(1);
                        fragment1.setData("init");
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        mIvAdvanceSearch.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        mIvAdvanceSearch.setVisibility(View.GONE);
                        break;
                }
                mTablayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int position) {
            }
        });

        mIvAdvanceSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PropertyFeeActivity.this, FeeAdvancedSearchActivity.class);
                startActivity(intent);
            }
        });
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0://初始化适配器
                    mTablayout.setTabData(mTitles);
                    mTablayout.setCurrentTab(0);

                    mAdapter = new PropertyFeeFragmentAdapter(getSupportFragmentManager(), mTitles);
                    mViewpager.setAdapter(mAdapter);
                    mViewpager.setOffscreenPageLimit(mTitles.length - 1);
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.mTitles = null;
        this.mAdapter = null;
        this.mHandler = null;
        this.mAppComponent = null;
    }
}
